<?php

/*-----------------------------------------------/
 说明：工具接口
/-------------------------------------------------*/



/*-----------------------------------------------/
 说明：处理上传的图片
参数：
$imgUrl:表单的图片参数
/-------------------------------------------------*/
function FileUpload($imgUrl = "imgUrl",$savePath=''){
	//判断附件是否有上传
	//如果有则实例化Upload，把附件上传到服务器指定位置
	//然后把附件的路径名获得到，存入$_POST
	if(!empty($_FILES)){
		$config = array(
				'rootPath'      =>     './Public/',  //根目录
				'savePath'      =>     '/admin/upload/', //保存路径
		);
		if($savePath){
			$config['savePath']=$savePath;
		}
		//附件被上传到路径：根目录/保存目录路径/创建日期目录
		$upload = new \Think\Upload($config);
		//uploadOne会返回已经上传的附件信息
		$z = $upload -> uploadOne($_FILES[$imgUrl]);
		if(!$z){
			$upload->getError(); //获得上传附件产生的错误信息
		}else {
			//拼装图片的路径名
			$bigimg = $z['savepath'].$z['savename'];
			$srcimg = $upload->rootPath.$bigimg;
			$_POST[$imgUrl] = $bigimg;
			return array(
					'absolute_'.$imgUrl => $srcimg,//全路劲
					'savepath'=>$z['savepath'],//保存的路劲
					'savename'=>$z['savename'],//保存图片的名字
					'rootPath'=>$upload->rootPath,//根目录
			);
		}
	}
}


/*-----------------------------------------------/
 说明：删除数据库的图片
参数：
$imgUrl:表单的图片参数
/-------------------------------------------------*/
function delfile($imgUrl = ""){
	if($imgUrl){//检查图片文件是否存在
		$filename = "./Public".$imgUrl;  //给图片地址拼接成完整路径
		$img_suffix = strrchr($imgUrl,'.');//获取图片后缀
		$img_suffix_name = array('.jpg','.JPG','.jpeg','.JPEG','.gif','.GIF','.png','.PNG');
		if(!in_array($img_suffix,$img_suffix_name)){//判断是不是图片资源
			return false;
		}
		if(file_exists($filename))//判断路径是不是存在,不存在也返回true无需删除
		{
			if(!unlink($filename)){//删除图片
				return false;	//删除失败!
			}
		}
	}
	return true;
}

/*-----------------------------------------------/
 说明：缓存多图上传图片
参数：
$imgUrl:图片路径
/-------------------------------------------------*/
function tmpUploadedImgUrl($imgUrl = ""){
	session("tmpUploads",$imgUrl);
	dump($_SESSION);
}

/*-----------------------------------------------/
 说明：截取字符中间的某段字符
 列子：
$string = "E:/wamp/www/gift/Public/home/uploads/tmpOploads/20150415-104432-07776logo4.png";
		$start = "/Public";
		$end = "";
echo get_from($string, $start, $end);  // output:/home/uploads/tmpOploads/20150415-104432-07776logo4.png
参数：
$input:待处理字符串, 
$start:第一次出现的字符串,获取在它之后的字符kk, 

/-------------------------------------------------*/
function get_from($input, $start,$end="") {
	$substr = substr($input, strlen($start)+strpos($input, $start));
	return $substr;
}

/*-----------------------------------------------/
 说明：截取字符中间的某段字符
列子：
$string = "www.jb51.net";
$start = "www.";
$end = ".net";
echo get_between($string, $start, $end);  // output:jb51
参数：
$input:待处理字符串,
$start:开始的字符串,
$end:接受的字符串
/-------------------------------------------------*/
function get_between($input, $start, $end) {
		$substr = substr($input, strlen($start)+strpos($input, $start),
				(strlen($input) - strpos($input, $end))*(-1));
	return $substr;
}


/*-----------------------------------------------/
 说明：删除编辑框的图片
参数：
$imgUrl:表单的编辑框参数
/-------------------------------------------------*/
//删除编辑框里面的图片
function delUeditorImg($content = "content"){
	$str = html_entity_decode($content);
	$html = str_get_html($str);
	foreach($html->find('img') as $element)
	{
		$res1 = $element->src;
		$res2 = substr_replace(__APP__,'',-9);
		$res3 = substr_replace($res1,'./',0,strlen($res2));
		$res = unlink($res3);
	}
}
/*-----------------------------------------------/
 说明：删除编辑框的附件
参数：
$imgUrl:表单的编辑框参数
/-------------------------------------------------*/
//删除编辑框里面的图片
function delUeditorFile($content = "content"){
	$str = html_entity_decode($content);
	$html = str_get_html($str);
	foreach($html->find('a') as $element)
	{
		$res1 = $element->href;
		$res2 = substr_replace(__APP__,'',-9);
		$res3 = substr_replace($res1,'./',0,strlen($res2));
		$res = unlink($res3);
	}
}

/**
* 函数用途描述:获取号码属性
* @date: 2016-4-6
* @author:RongShu
* @param:$number:号码,字符串，多个号码以逗号隔开；$tm:是否需要获取特码属性；$date:年月日，格式Y-m-d（2016-04-07）
* @return:$result:数组，包含所有属性
*/
function getNumberAll($number,$tm=FALSE,$date=NULL){
	//获取新历
  	if($date==null){
  		$new_year = explode("-",date('Y-m-d',time()));
  	}else {
  		$new_year = explode("-",$date);
  	}
  	if(!$new_year){
  		die("年分格式有误！");
  	}
  	//获取农历和生肖年
	$lunar = new \Org\Util\Lunar();
	$lunar_year = $lunar->convertSolarToLunar($new_year[0],$new_year[1],$new_year[2]);
	//获取中奖号码数组
	$nums = explode(",",$number);
	foreach ($nums as $key => $value) {
		$sx = getNumberSx($value,$lunar_year[6],$tm);//获取生肖波色等其他属性
		$res[$key]=$sx;
		$res[$key]['number']=$value;
		$res[$key]['old_time']=$lunar_year;
	}
	return $res;
}
//获取生肖
function getNumberSx($num,$year_sx,$tm=false){
	//生肖
	$num_sx='';
	$sx_num = $num%12 ;
	switch ($year_sx) {
		case '猴':
			$array_sx =array('猴','羊','马','蛇','龙','兔','虎','牛','鼠','猪','狗','鸡');
			if($sx_num==0){
				$num_sx=$array_sx[11];
			}else {
				$num_sx=$array_sx[$sx_num-1];
			}
			break;
		case '鸡':
			$array_sx =array('鸡','猴','羊','马','蛇','龙','兔','虎','牛','鼠','猪','狗');
			if($sx_num==0){
				$num_sx=$array_sx[11];
			}else {
				$num_sx=$array_sx[$sx_num-1];
			}
			break;
		case '狗':
			$array_sx =array('狗','鸡','猴','羊','马','蛇','龙','兔','虎','牛','鼠','猪',);
			if($sx_num==0){
				$num_sx=$array_sx[11];
			}else {
				$num_sx=$array_sx[$sx_num-1];
			}
			break;
		case '猪':
			$array_sx =array('猪','狗','鸡','猴','羊','马','蛇','龙','兔','虎','牛','鼠',);
			if($sx_num==0){
				$num_sx=$array_sx[11];
			}else {
				$num_sx=$array_sx[$sx_num-1];
			}
			break;
		case '鼠':
			$array_sx =array('鼠','猪','狗','鸡','猴','羊','马','蛇','龙','兔','虎','牛',);
			if($sx_num==0){
				$num_sx=$array_sx[11];
			}else {
				$num_sx=$array_sx[$sx_num-1];
			}
			break;
		case '牛':
			$array_sx =array('牛','鼠','猪','狗','鸡','猴','羊','马','蛇','龙','兔','虎',);
			if($sx_num==0){
				$num_sx=$array_sx[11];
			}else {
				$num_sx=$array_sx[$sx_num-1];
			}
			break;
		case '虎':
			$array_sx =array('虎','牛','鼠','猪','狗','鸡','猴','羊','马','蛇','龙','兔',);
			if($sx_num==0){
				$num_sx=$array_sx[11];
			}else {
				$num_sx=$array_sx[$sx_num-1];
			}
			break;
		case '兔':
			$array_sx =array('兔','虎','牛','鼠','猪','狗','鸡','猴','羊','马','蛇','龙',);
			if($sx_num==0){
				$num_sx=$array_sx[11];
			}else {
				$num_sx=$array_sx[$sx_num-1];
			}
			break;
		case '龙':
			$array_sx =array('龙','兔','虎','牛','鼠','猪','狗','鸡','猴','羊','马','蛇',);
			if($sx_num==0){
				$num_sx=$array_sx[11];
			}else {
				$num_sx=$array_sx[$sx_num-1];
			}
			break;
		case '蛇':
			$array_sx =array('蛇','龙','兔','虎','牛','鼠','猪','狗','鸡','猴','羊','马',);
			if($sx_num==0){
				$num_sx=$array_sx[11];
			}else {
				$num_sx=$array_sx[$sx_num-1];
			}
			break;
		case '马':
			$array_sx =array('马','蛇','龙','兔','虎','牛','鼠','猪','狗','鸡','猴','羊',);
			if($sx_num==0){
				$num_sx=$array_sx[11];
			}else {
				$num_sx=$array_sx[$sx_num-1];
			}
			break;
		case '羊':
			$array_sx =array('羊','马','蛇','龙','兔','虎','牛','鼠','猪','狗','鸡','猴',);
			if($sx_num==0){
				$num_sx=$array_sx[11];
			}else {
				$num_sx=$array_sx[$sx_num-1];
			}
			break;
		default:
			echo "获取生肖有误!";
		break;
	}
	
	//波色
	$array_bs_red =    array('01','02','07','08','12','13','18','19','23','24','29','30','34','35','40','45','46');
	$array_bs_bule =  array('03','04','09','10','14','15','20','25','26','31','36','37','41','42','47','48');
	$array_bs_green =array('05','06','11','16','17','21','22','27','28','32','33','38','39','43','44','49');
	if(in_array($num, $array_bs_red)){
		$num_bs="红";
	}elseif (in_array($num, $array_bs_bule)){
		$num_bs="蓝";
	}elseif (in_array($num, $array_bs_green)){
		$num_bs="绿";
	}else {
		echo "波色有误";
	}
	
	//特码获取更多属性
	if($tm){
		//单双
		if(($num%2)==0){
			$num_ds='双';
		}else{
			$num_ds='单';
		}
		//大小
		if($num>24){
			$num_dx='大';
		}else{
			$num_dx='小';
		}
		//五行
		$array_wx_jin =    array('01','06','11','16','21','26','31','36','41','46');
		$array_wx_mu =   array('02','07','12','17','22','27','32','37','42','47');
		$array_wx_shui = array('03','08','13','18','23','28','33','38','43','48');
		$array_wx_huo =  array('04','09','14','19','24','29','34','39','44','49');
		$array_wx_tu =     array('05','10','15','20','25','30','35','40','45');
		if(in_array($num, $array_wx_jin)){
			$num_wx="金";
		}elseif (in_array($num, $array_wx_mu)){
			$num_wx="木";
		}elseif (in_array($num, $array_wx_shui)){
			$num_wx="水";
		}elseif (in_array($num, $array_wx_huo)){
			$num_wx="火";
		}elseif (in_array($num, $array_wx_tu)){
			$num_wx="土";
		}else {
			echo "五行有误";
		}
		//特头尾数
		$tt=substr($num,0,1);
		$num_tt=$tt."头";
		$ws=substr($num,-1);
		$num_ws=$ws."尾";
		//合单双
		$array_hds_d =  array('01','03','05','07','09','10','12','14','16','18','21','23','25','27','29','30','32','34','36','38','41','43','45','47','49');
		$array_hds_s  =  array('02','04','06','08','10','11','13','15','17','19','20','22','24','26','28','31','33','35','37','39','40','42','44','46','48');
		if(in_array($num, $array_hds_d)){
			$num_hds="合单";
		}elseif (in_array($num, $array_hds_s)){
			$num_hds="合双";
		}else {
			echo "合单双有误";
		}
		//家野
		$array_jy_jq=array('牛','马','羊','鸡','狗','猪');
		$array_jy_ys=array('鼠','虎','兔','龙','蛇','猴');
		if(in_array($num_sx, $array_jy_jq)){
			$num_jy="家禽";
		}elseif (in_array($num_sx, $array_jy_ys)){
			$num_jy="野兽";
		}else {
			echo "家野有误";
		}
		//门数
		if($num>=1 && $num<=9){
			$num_ms="1门";
		}elseif ($num>=10 && $num<=18){
			$num_ms="2门";
		}elseif ($num>=19 && $num<=27){
			$num_ms="3门";
		}elseif ($num>=28 && $num<=37){
			$num_ms="4门";
		}elseif ($num>=38 && $num<=49){
			$num_ms="5门";
		}else {
			echo "门数有误";
		}
		//段位
		if($num>=1 && $num<=7){
			$num_dw="1段";
		}elseif ($num>=8 && $num<=14){
			$num_dw="2段";
		}elseif ($num>=15 && $num<=21){
			$num_dw="3段";
		}elseif ($num>=22 && $num<=28){
			$num_dw="4段";
		}elseif ($num>=29 && $num<=35){
			$num_dw="5段";
		}elseif ($num>=36 && $num<=42){
			$num_dw="6段";
		}elseif ($num>=43 && $num<=49){
			$num_dw="7段";
		}else {
			echo "段位有误";
		}
		//阴阳
		$array_yy_yi=array('鼠','龙','蛇','马','狗','猪');
		$array_yy_ya=array('牛','虎','兔','羊','猴','鸡');
		if(in_array($num_sx, $array_yy_yi)){
			$num_yy="阴";
		}elseif (in_array($num_sx, $array_yy_ya)){
			$num_yy="阳";
		}else {
			echo "阴阳有误";
		}
		//天地
		$array_td_t=array('兔','马','猴','猪','牛','龙');
		$array_td_d=array('蛇','羊','鸡','狗','鼠','虎');
		if(in_array($num_sx, $array_td_t)){
			$num_td="天";
		}elseif (in_array($num_sx, $array_td_d)){
			$num_td="地";
		}else {
			echo "天地有误";
		}
		//吉凶
		$array_jx_j=array('兔','龙','蛇','马','羊','鸡');
		$array_jx_x=array('鼠','牛','虎','猴','狗','猪');
		if(in_array($num_sx, $array_jx_j)){
			$num_jx="吉";
		}elseif (in_array($num_sx, $array_jx_x)){
			$num_jx="凶";
		}else {
			echo "吉凶有误";
		}
		//黑白
		$array_hb_b=array('鼠','牛','虎','鸡','狗','猪');
		$array_hb_h=array('兔','龙','蛇','马','羊','猴');
		if(in_array($num_sx, $array_hb_b)){
			$num_hb="白";
		}elseif (in_array($num_sx, $array_hb_h)){
			$num_hb="黑";
		}else {
			echo "黑白有误";
		}
		//色肖
		$array_sex_red=array('马','兔','鼠','鸡');
		$array_sex_bule=array('蛇','虎','猪','猴');
		$array_sex_green=array('羊','龙','牛','狗');
		if(in_array($num_sx, $array_sex_red)){
			$num_sex="红肖";
		}elseif (in_array($num_sx, $array_sex_bule)){
			$num_sex="蓝肖";
		}elseif (in_array($num_sx, $array_sex_green)){
			$num_sex="绿肖";
		}else {
			echo "色肖有误";
		}
		//笔画
		$array_bh_d=array('鼠','龙','马','蛇','鸡','猪');
		$array_bh_s=array('虎','猴','狗','兔','羊','牛');
		if(in_array($num_sx, $array_bh_d)){
			$num_bh="单笔";
		}elseif (in_array($num_sx, $array_bh_s)){
			$num_bh="双笔";
		}else {
			echo "笔画有误";
		}
		//男女
		$array_nv_v=array('兔','蛇','羊','鸡','猪');
		$array_nv_n=array('鼠','牛','虎','龙','马','猴','狗');
		if(in_array($num_sx, $array_nv_v)){
			$num_nv="女";
		}elseif (in_array($num_sx, $array_nv_n)){
			$num_nv="男";
		}else {
			echo "男女有误";
		}
		
		$res['ds']=$num_ds;//单双
		$res['dx']=$num_dx;//大小
		$res['wx']=$num_wx;//五行
		$res['tt']=$num_tt;//特头
		$res['ws']=$num_ws;//尾数
		$res['hds']=$num_hds;//合单双
		$res['jy']=$num_jy;//家野
		$res['ms']=$num_ms;//门数
		$res['dw']=$num_dw;//段位
		$res['yy']=$num_yy;//阴阳
		$res['td']=$num_td;//天地
		$res['jx']=$num_jx;//吉凶
		$res['hb']=$num_hb;//天地
		$res['sex']=$num_sex;//天地
		$res['bh']=$num_bh;//色肖
		$res['nv']=$num_nv;//男女
	}
	$res['sx']=$num_sx;//生肖
	$res['bs']=$num_bs;//波色
	
	return $res;
}

function getNumberAndSx($date=null){
	if($date==null){
		$new_year = explode("-",date('Y-m-d',time()));
	}else {
		$new_year = explode("-",$date);
	}
	if(!$new_year[0]||!$new_year[1]||!$new_year[2]){
		die("年分格式有误！");
	}
	//获取农历和生肖年
	$lunar = new \Org\Util\Lunar();
	$lunar_year = $lunar->convertSolarToLunar($new_year[0],$new_year[1],$new_year[2]);
	switch ($lunar_year[6]) {
		case '猴':
			$array_sx =array('猴','羊','马','蛇','龙','兔','虎','牛','鼠','猪','狗','鸡');
			break;
		case '鸡':
			$array_sx =array('鸡','猴','羊','马','蛇','龙','兔','虎','牛','鼠','猪','狗');
			break;
		case '狗':
			$array_sx =array('狗','鸡','猴','羊','马','蛇','龙','兔','虎','牛','鼠','猪',);
			break;
		case '猪':
			$array_sx =array('猪','狗','鸡','猴','羊','马','蛇','龙','兔','虎','牛','鼠',);
			break;
		case '鼠':
			$array_sx =array('鼠','猪','狗','鸡','猴','羊','马','蛇','龙','兔','虎','牛',);
			break;
		case '牛':
			$array_sx =array('牛','鼠','猪','狗','鸡','猴','羊','马','蛇','龙','兔','虎',);
			break;
		case '虎':
			$array_sx =array('虎','牛','鼠','猪','狗','鸡','猴','羊','马','蛇','龙','兔',);
			break;
		case '兔':
			$array_sx =array('兔','虎','牛','鼠','猪','狗','鸡','猴','羊','马','蛇','龙',);
			break;
		case '龙':
			$array_sx =array('龙','兔','虎','牛','鼠','猪','狗','鸡','猴','羊','马','蛇',);
			break;
		case '蛇':
			$array_sx =array('蛇','龙','兔','虎','牛','鼠','猪','狗','鸡','猴','羊','马',);
			break;
		case '马':
			$array_sx =array('马','蛇','龙','兔','虎','牛','鼠','猪','狗','鸡','猴','羊',);
			break;
		case '羊':
			$array_sx =array('羊','马','蛇','龙','兔','虎','牛','鼠','猪','狗','鸡','猴',);
			break;
		default:
			echo "获取生肖有误!";
			break;
	}
	return $array_sx;
}

function  getNewNumber(){
	$code_Model = D("Code");
	$result = $code_Model->order("date desc")->field('date_num')->find();
	return (substr_replace("000",($result['date_num']+1),-strlen(($result['date_num']+1))));
}

function  getNewCokeKey(){
	$code_Model = D("Code");
	$result = $code_Model->order("date desc")->field('code_key')->find();
	return $result['code_key']+1;
}

function  getNowCokeDate(){
	$code_Model = D("Code");
	$result = $code_Model->order("date desc")->field('date')->find();
	return substr($result['date'],0,7);//只需返回年月2016-08
}

function getNextId($nowId){
// 	$nowId='20160919056';
	if(substr($nowId,8) == 120){
		$now_time = substr($nowId, 0,8);
		$id_new = date('Ymd',86400+strtotime($now_time));//加多一天
// 					dump($id_new);
		$id=$id_new.'001';//新一天从第一期开始
	}else{
		$id=$nowId+1;
	}
	return $id;
}

function get_rand(){
	return rand(10,30);
}

function checkIsSx($string){
	$array_sx =array('猴','羊','马','蛇','龙','兔','虎','牛','鼠','猪','狗','鸡');
	if(in_array($string, $array_sx)){
		return true;
	}else {
		return false;
	}
}

function checkIsHm($string){
	$array_hm = array('1','2','7','8','12','13','18','19','23','24','29','30','34','35','40','45','46','3','4','9','10','14','15','20','25','26','31','36','37','41','42','47','48',
	'5','6','11','16','17','21','22','27','28','32','33','38','39','43','44','49');
	if(in_array($string, $array_hm,true)){
		return true;
	}else {
		return false;
	}
}


// function getNextId($code_key){
// 	$code_Model = D("Code");
// 	$result = $code_Model->order("date desc")->field('date')->find();
// 	return substr($result['date'],0,7);//只需返回年月2016-08
// }

function test(){}


